package com.muki.succ.registery;

import org.apache.log4j.Logger;

import com.muki.spider.Spider;
import com.muki.succ.uri.UriQueueManagerPool;

public class SpiderCore implements Runnable {

	private final Logger log = Logger.getLogger(SpiderCore.class);
	private UriQueueManagerPool pool = UriQueueManagerPool.getInstance();
	private Spider spider;
	private String topic;

	public SpiderCore(Spider spider) {
		this.spider=spider;
		this.topic = spider.getTopic();
	}
	
	public static SpiderCore newInstance(Spider spider) {
		return new SpiderCore(spider);
	}

	public void executeTask() {
		try {
			String uri = pool.getUrl(topic);
			spider.crawling(uri);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {

		spider.setStatus(true);
		while(!Thread.currentThread().isInterrupted()) {
			executeTask();
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		spider.setStatus(false);
	}

}
